﻿Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.General
Imports DevComponents.DotNetBar
Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.DAO

Public Class frmNhaCungCap
    Private m_QuyDinh As New QuyDinh()
    Private m_NhaCungCapCtrl As New NhaCungCapCtrl()
    Public Sub New()
        InitializeComponent()
        DataService.OpenConnection()
    End Sub

    Private Sub frmNhaCungCap_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        m_NhaCungCapCtrl.HienThi(DGVNhaCungCap, BindingNavigatorNhaCungCap, txtMaNCC, txtTenNhaCC, txtEmail, txtDiaChi, _
    txtFax, txtGhiChu, txtDienThoai)

    End Sub

    Private Sub bindingNavigatorAddNewItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorAddNewItem.Click
        If DGVNhaCungCap.RowCount = 0 Then
            BindingNavigatorNhaCungCap.Enabled = True
        End If
        Dim m_Row As DataRow = m_NhaCungCapCtrl.ThemDongMoi()
        m_Row("MaNCC") = "NCC" & m_QuyDinh.LaySTT(DGVNhaCungCap.Rows.Count + 1)
        m_Row("TenNCC") = ""
        m_Row("DiaChi") = ""
        m_Row("Email") = ""
        m_Row("Fax") = ""
        m_Row("DienThoai") = ""
        m_Row("GhiChu") = ""

        m_NhaCungCapCtrl.ThemMoi(m_Row)
        BindingNavigatorNhaCungCap.BindingSource.MoveLast()

    End Sub

    Private Sub bindingNavigatorRefreshItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorRefreshItem.Click
        txtTimKiem.Text = ""
        m_NhaCungCapCtrl.HienThi(DGVNhaCungCap, BindingNavigatorNhaCungCap, txtMaNCC, txtTenNhaCC, txtEmail, txtDiaChi, _
         txtFax, txtGhiChu, txtDienThoai)

    End Sub

    Public Function KiemTraTruocKhiLuu(str As [String]) As [Boolean]
        For Each row As DataGridViewRow In DGVNhaCungCap.Rows
            If row.Cells(str).Value IsNot Nothing Then
                Dim m_Str As [String] = row.Cells(str).Value.ToString()
                If m_Str = "" Then
                    Return False
                End If
            End If
        Next
        Return True
    End Function

    Private Sub bindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorSaveItem.Click
        Try
            If KiemTraTruocKhiLuu("colMaNCC") = True AndAlso KiemTraTruocKhiLuu("colTenNCC") = True AndAlso KiemTraTruocKhiLuu("colDiaChi") = True AndAlso KiemTraTruocKhiLuu("colDienThoai") = True AndAlso KiemTraTruocKhiLuu("colFax") = True AndAlso KiemTraTruocKhiLuu("colEmail") = True Then
                bindingNavigatorPositionItem.Focus()
            End If
            m_NhaCungCapCtrl.LuuNhaCungCap()
        Catch
            MessageBoxEx.Show("Giá trị ô không được rỗng!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try

    End Sub

    Private Sub bindingNavigatorDeleteItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorDeleteItem.Click
        If DGVNhaCungCap.RowCount = 0 Then
            bindingNavigatorDeleteItem.Enabled = False

        ElseIf MessageBox.Show("Bạn có chắc chắn xóa dòng này không?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            BindingNavigatorNhaCungCap.BindingSource.RemoveCurrent()
        End If

    End Sub

    Private Sub bindingNavigatorCloseItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorCloseItem.Click
        Me.Close()

    End Sub

#Region "Tìm kiếm nhà cung cấp"
    Private Sub btnSearch_KeyDown(sender As Object, e As KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            TimKiemNhaCungCap()
        End If
    End Sub

    Private Sub TimKiemNhaCungCap()
        Try
            If cbMaNCC.Checked = True Then
                m_NhaCungCapCtrl.TimKiemTheoMa(txtTimKiem.Text)
            ElseIf cbTenNCC.Checked = True Then
                m_NhaCungCapCtrl.TiemKiemTheoTen(txtTimKiem.Text)
            End If
        Catch
            MessageBoxEx.Show("Không thể tìm thấy Giá trị cần tìm!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try
    End Sub

    Private Sub btnTimKiem_Click(sender As System.Object, e As System.EventArgs) Handles btnTimKiem.Click
        If txtTimKiem.Text = "" Then
            MessageBoxEx.Show("Giá trị tìm rỗng!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        Else
            TimKiemNhaCungCap()
        End If
    End Sub
#End Region

    Private Sub buttonSave_Click(sender As System.Object, e As System.EventArgs) Handles buttonSave.Click
        Try
            If KiemTraTruocKhiLuu("colMaNCC") = True AndAlso KiemTraTruocKhiLuu("colTenNCC") = True AndAlso KiemTraTruocKhiLuu("colDiaChi") = True AndAlso KiemTraTruocKhiLuu("colDienThoai") = True AndAlso KiemTraTruocKhiLuu("colFax") = True AndAlso KiemTraTruocKhiLuu("colEmail") = True Then
                bindingNavigatorPositionItem.Focus()
            End If
            m_NhaCungCapCtrl.LuuNhaCungCap()
            MessageBoxEx.Show("Đã lưu thành công!", "Hoàn Thành", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch
            MessageBoxEx.Show("Phải nhập đầy đủ dữ liệu để thêm nhà cung cấp", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try

    End Sub

    Private Sub btnThemNCC_Click(sender As System.Object, e As System.EventArgs) Handles btnThemNCC.Click
        If DGVNhaCungCap.RowCount = 0 Then
            BindingNavigatorNhaCungCap.Enabled = True
        End If
        Dim m_Row As DataRow = m_NhaCungCapCtrl.ThemDongMoi()
        m_Row("MaNCC") = "NCC" & m_QuyDinh.LaySTT(DGVNhaCungCap.Rows.Count + 1)
        m_Row("TenNCC") = ""
        m_Row("DiaChi") = ""
        m_Row("Email") = ""
        m_Row("Fax") = ""
        m_Row("DienThoai") = ""
        m_Row("GhiChu") = ""
        m_NhaCungCapCtrl.ThemMoi(m_Row)
        BindingNavigatorNhaCungCap.BindingSource.MoveLast()

    End Sub

    Private Sub btnSua_Click(sender As System.Object, e As System.EventArgs) Handles btnSua.Click
        m_NhaCungCapCtrl.SuaNhaCungCap(txtMaNCC.Text, txtTenNhaCC.Text, txtDienThoai.Text, txtFax.Text, txtEmail.Text, txtDiaChi.Text, _
            txtGhiChu.Text)
        m_NhaCungCapCtrl.HienThi(DGVNhaCungCap, BindingNavigatorNhaCungCap, txtMaNCC, txtTenNhaCC, txtEmail, txtDiaChi, _
         txtFax, txtGhiChu, txtDienThoai)
        MessageBoxEx.Show("Đã sửa thành công!", "Hoàn Thành", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub btnDel_Click(sender As System.Object, e As System.EventArgs) Handles btnDel.Click
        If MessageBoxEx.Show("Bạn có chắc chắn xóa nhà cung cấp này không?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            m_NhaCungCapCtrl.XoaNhaCungCap(txtMaNCC.Text)
            m_NhaCungCapCtrl.HienThi(DGVNhaCungCap, BindingNavigatorNhaCungCap, txtMaNCC, txtTenNhaCC, txtEmail, txtDiaChi, _
             txtFax, txtGhiChu, txtDienThoai)
        End If

    End Sub

    Private Sub btnThem_Click(sender As System.Object, e As System.EventArgs) Handles btnThem.Click
        LoadMain.HienThiMatHang()

    End Sub

    Private Sub btnHuy_Click(sender As System.Object, e As System.EventArgs) Handles btnHuy.Click
        Me.Close()

    End Sub
End Class